<?php
/**
 * Punny - The most easy-to-use PHP MVC framework
 * 
 * Copyright (c) 2010  
 *
 * @author email :lx.xin@qq.com
 */
 require ('BaseController.php');
 class Admin_BlogController extends Admin_BaseController{
	var $blog ;
	function init(){
		parent::init();
		$this->blog = new Blog();
	}
	/*
	 *显示日志列表
	 */
	function indexAction (){
		$numPerPage = 10;
		$getPage = $this->_request->getParam('page');
		$page = empty($getPage)?'1':$getPage;
		//抓回所有要显示的记录
		$array=$this->blog->blogList();
		$classes = new Classes();
		$c_arr=$classes->getClasses();
		if(empty($array)){
			$this->view->url=$this->_request->getBaseUrl()."/admin/blog/add/";
			$this->view->msg="数据不存在";
			echo $this->view->render('error.html');
		}
		//Zend_debug::dump($class);		
		$blog_arr=array();
		foreach($array as $key=>$value){
			$blog_arr[$key]['id'] = $value['id'];			
			$blog_arr[$key]['title'] = $value['title'];	
			foreach ($c_arr as $key1 =>$val){
				if($value['classid']==$val['id']){
					$blog_arr[$key]['classname'] = $val['classname'];
				}
			}						
			$blog_arr[$key]['posttime'] = date('Y-m-d',$value['posttime']);
			$blog_arr[$key]['commnums'] = $value['commnums'];
			
		}

		//Zend_debug::dump($q);
		//factory出一个分页并且设置当前页码与每页显数
		$paginator = Zend_Paginator::factory($blog_arr);
		$paginator->setCurrentPageNumber($page)
				  ->setItemCountPerPage($numPerPage);
         //把分页显示到view
		$this->view->paginator = $paginator;		
		echo $this->view->render('blog.html');
	}
	/*
	 *添加日志
	 */
	 function addAction(){
		if(isset($_POST['add'])){
			$filter=array(
				'*'=>'StringTrim'	
			);
			$validate=array(
				'title'=>array(
					array('StringLength',1,150),
					Zend_Filter_Input::MESSAGES=>array(array(
						Zend_Validate_StringLength::INVALID=>"请输入一个合法的字符串",
						Zend_Validate_StringLength::TOO_SHORT=>"请输入字符长度为1-150",
						Zend_Validate_StringLength::TOO_LONG=>"请输入字符长度为1-150"
			))
			)		
			);
			$input=new Zend_Filter_Input($filter,$validate,$this->getRequest()->getPost());
			if($input->isValid()){
				$data=array_merge($input->getEscaped(),$input->getUnknown());				
				unset($data['add']);				
				isset($data['iscomment']) ? $data['iscomment']=1 : $data['iscomment']=0;
				isset($data['ishidden']) ? $data['ishidden']=1 : $data['ishidden']=0 ;
				isset($data['iszd']) ? $data['iszd']=1 : $data['iszd']=0 ;
				//var_dump($data);
				$data['posttime']=time();
				$data['author'] =$_SESSION['username'];
				$this->blog->addBlog($data);//添加文章
				$tag = new Tag();
				if(isset($data['tags'])){
					$tag_arr = explode(',',$data['tags']);
					//var_dump($tag_arr);
					foreach ($tag_arr as $value){
						$tag->addTag($value);
					}
				}
				$this->view->url=$this->_request->getBaseUrl()."/admin/blog/index/";
				$this->view->msg="添加成功";
				echo  $this->view->render('success.html');
			}
			else{
				$this->view->errors=$input->getMessages();
				$this->view->url=$this->_request->getBaseUrl()."/admin/blog/add/";
				$this->view->msg="添加失败";
				echo $this->view->render('error.html');
			}
		}
		else{
			$classes= new Classes();
			$this->view->arrclass = $classes->getClasses();
			$this->view->x='add';
			$this->fckAction();
			echo $this->view->render('blog.html');
		}
	 }
	 /*
	  *编辑	
	  */
	  function editAction(){
		if(isset($_POST['edit'])){
			$id=$this->_request->get('id');
			$data=$this->_request->getPost();
			//var_dump($data);
			isset($data['iscomment']) ? $data['iscomment']=1 : $data['iscomment']=0;
			isset($data['ishidden']) ? $data['ishidden']=1 : $data['ishidden']=0 ;
			isset($data['iszd']) ? $data['iszd']=1 : $data['iszd']=0 ;			
			unset($data['edit']);
			$this->blog->updateBlog($id,$data);
			$this->view->url=$this->_request->getBaseUrl()."/admin/blog/";
			$this->view->msg="编辑成功";
			echo $this->view->render('success.html');			
		}
		else{
			$id=$this->_request->get('id');
			if($id){
				$data=$this->blog->queryBlog($id);
				$class = new Classes();
				$arr=$class->getClasses();//分类
				$this->view->id=$data['id'];
				$this->view->classid=$data['classid'];
				$this->view->title=$data['title'];
				$this->view->arrclass=$arr;			
				$this->view->tags=$data['tags'];//需优化，若新添加tag等更新相关操作，暂时先不做处理。
				$this->view->iscomment=$data['iscomment'];
				$this->view->ishidden=$data['ishidden'];
				$this->view->iszd=$data['iszd'];
				$this->view->x='edit';
				$this->fckAction($data['content']);
				echo $this->view->render('blog.html');
			}
			else{
				$this->view->url=$this->_request->getBaseUrl()."/admin/blog/";
				$this->view->msg="编辑的数据不存在";
				echo $this->view->render('error.html');
			}
		}
			
	  }

	  /*
	   *删除日志
	   */
	   function delAction (){
		$id=$this->_request->get('id');
		if($id>0){
			$this->blog->delBlog($id);
			$this->view->url=$this->_request->getBaseUrl()."/admin/blog/";
			$this->view->msg="删除成功";
			echo $this->view->render('success.html');
		}
		else{
			$this->view->url=$this->_request->getBaseUrl()."/admin/blog/";
			$this->view->msg="删除失败";
			echo $this->view->render('error.html');
		}
	   }
	 /*
	  *fck编辑器
	  */
	 function fckAction ($content=''){
		//加载FCK类
		Zend_Loader::loadClass('Common_Plugin_Fckeditor');

		//实例化这个类
		$oFCKeditor = new Common_Plugin_Fckeditor('content') ;

		//这里是放置FCK文件的路径.
		$oFCKeditor->BasePath = $this->view->baseUrl."/public/fckeditor/";

		//FCK皮肤
		$oFCKeditor->Config['SkinPath'] =
		$this->view->baseUrl.'/public/fckeditor/editor/skins/silver/' ;
		$oFCKeditor->Width = '100%';
		$oFCKeditor->Height = '500';
		$oFCKeditor->Value = $content;
		$this->view->editor = $oFCKeditor->CreateHtml();
	 }	
	 

 }
 ?>